package sg.edu.nus.iss.ooad.jass.ui.widget;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import sg.edu.nus.iss.ooad.jass.ui.JASSAppFrame;

/**
 *
 * Template Regular Window for JASS.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : AbstractJASSRegularWindow.java
 * Creation / Modification History
 *         Isak Rabin        03-Oct-2009        Created
 *
 */
public abstract class AbstractJASSRegularWindow extends Frame {

        private static final long serialVersionUID = -6378515361106403604L;

        /**
         * Constructor
         * @param title Frame title
         * @param dimension Frame Dimension
         */
        public AbstractJASSRegularWindow(String title, Dimension dimension) {
                super(title);
                setSize(dimension);
                setCenteredToScreen();
                addWindowListener(new WindowAdapter() {

                        @Override
                        public void windowClosing(WindowEvent e) {
                                close();
                                if (e.getSource() instanceof JASSAppFrame) {
                                        System.exit(0);
                                }
                        }
                });
                setLayout(new GridBagLayout());
        }

        /**
         * Constructor
         * @param title Frame title
         * @param width Frame width
         * @param height Fram height
         */
        public AbstractJASSRegularWindow(String title, int width, int height) {
                this(title, new Dimension(width, height));
        }

        /**
         * This operation will reset form entry field.
         */
        public abstract void resetForm();

        /**
         * An abstract method for JASS Frame to arrange the component to GridBagLayout.
         * {@link #attachComponent(Component c, int gridx, int gridy, int gridwidth, int gridheight)}
         * @param c
         * @param gridx
         * @param gridy
         * @param gridwidth
         * @param gridheight
         * @param fill
         */
        public abstract void attachComponent(Component c, int gridx, int gridy, int gridwidth, int gridheight, int fill);

        /**
         * Attach component to the GridBagLayout.
         * @param c
         * @param gridx
         * @param gridy
         * @param gridwidth
         * @param gridheight
         */
        public void attachComponent(Component c, int gridx, int gridy, int gridwidth, int gridheight) {
                attachComponent(c, gridx, gridy, gridwidth, gridheight, GridBagConstraints.BOTH);
        }

        /**
         * This operation will close the window.
         */
        public void close() {
                setVisible(false);
                dispose();
        }

        /**
         * 
         * @param title
         * @param message
         */
        public void displayMessage(String title, String message) {
                final JASSDialogWindow dia = new JASSDialogWindow(this, title, new Dimension(280, 100), true, message);
                dia.dialogActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                dia.setVisible(false);
                                dia.dispose();
                        }
                });
                dia.setLocationRelativeTo(this);
                dia.setVisible(true);
                dia.toFront();
        }

        /**
         * This operation will set the frame position to the center of screen
         */
        private void setCenteredToScreen() {
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

                double x = (screenSize.getWidth() - getWidth()) / 2;
                double y = (screenSize.getHeight() - getHeight()) / 2;

                setLocation((int) (x), (int) (y));
        }
}

